<?php

namespace App\Services\Game\Setting;

use App\Services\Service;
use App\Models\Game\Platform\Org;
use App\Models\Game\Config\Game;
use App\Models\Game\Config\System as Config;
use App\Models\Game\Config\Broadcast\Broadcast;
use App\Models\Game\Common\Dictionary;
use Illuminate\Http\Request;

class MaintenanceService extends Service
{

    /**
     * 全服开关
     *
     * @param Config|null $model
     * @return array
     */
    protected function getServerFull(?Config $model = null): array
    {
        $config = $model ?: Config::findByType(Config::TYPE_SERVER_FULL);

        return $config->info ?? ['enable' => Config::ENABLE_ON];
    }

    /**
     * 关闭游戏
     *
     * @param Config|null $model
     * @return array
     */
    protected function getServerGame(?Config $model = null): array
    {
        $config = $model ?: Config::findByType(Config::TYPE_SERVER_GAME);

        return Game::whereIn('code', $config->info ?? [])->get()->toArray();
    }

    /**
     * 关闭平台
     *
     * @param Config|null $model
     * @return array
     */
    protected function getServerPlatform(?Config $model = null): array
    {
        $config = $model ?: Config::findByType(Config::TYPE_SERVER_PLATFORM);

        return Org::whereIn('code', $config->info ?? [])->get()->toArray();
    }

    /**
     * 获取游戏服状态
     *
     * @return array
     */
    public function getServerStatus(): array
    {
        return [
            // 全服开关
            Config::TYPE_SERVER_FULL => $this->getServerFull(),
            // 关闭游戏
            Config::TYPE_SERVER_GAME => $this->getServerGame(),
            // 关闭平台
            Config::TYPE_SERVER_PLATFORM => $this->getServerPlatform(),
        ];
    }

    /**
     * 更新游戏服状态
     *
     * @param Request $request
     * @return array
     */
    public function updateServerStatus(Request $request): array
    {
        $config = Config::updateOrCreateByType(
            $type = $request->input('type'),
            $info = $request->input($type)
        );

        // 通知游戏端配置修改
        $config->syncGameService();

        // 分类处理
        switch ($type) {
            case Config::TYPE_SERVER_FULL:
                return $this->getServerFull($config);
            case Config::TYPE_SERVER_GAME:
                return $this->getServerGame($config);
            case Config::TYPE_SERVER_PLATFORM:
                return $this->getServerPlatform($config);
        }
    }

    /**
     * 获取游戏公告
     *
     * @param Request $request
     * @return Broadcast|null
     */
    public function getBroadcast(Request $request): ?Broadcast
    {
        return Broadcast::where('plat_code', Org::ALL_CODE)->with('contents:id,content')->first();
    }

    /**
     * 发布游戏公告
     *
     * @param Request $request
     * @return Broadcast
     */
    public function publishBroadcast(Request $request): Broadcast
    {
        // 验证内容和参数匹配
        Dictionary::validate(
            $request->input('content_ids', []),
            $request->input('content_param', [])
        );

        // 修改或创建数据
        $broadcast = Broadcast::updateOrCreate(
            ['plat_code' => Org::ALL_CODE],
            $request->only(['enable', 'expire_time', 'content_param'])
        );

        // 同步关联内容
        $broadcast->contents()->sync($request->input('content_ids', []));

        // 通知游戏端配置修改
        $broadcast->syncGameService();

        // 加载关联内容
        $broadcast->load('contents:id,content');

        return $broadcast;
    }
}
